﻿using System;
using System.Collections.Generic;
using System.Data;
using MigSharp;

namespace CmsDatabaseMigrations.Migrations
{
    [MigrationExport(ModuleName = "Splits UserName field to Firtsname and Lastname. Then adds UserName as computed fields", Tag = "Users")]
    public class UsersSplitUserName201206301518 : BatchMigration
    {
        protected override IEnumerable<string> SpFileNames
        {
            get
            {
                yield return "cms_sp_CreateUser201206301518.sql";
                yield return "cms_sp_GetUserAndUserGroups201206301518.sql";
                yield return "cms_sp_GetUserForManagement201206301518.sql";
                yield return "cms_sp_GetUsersForManagement201206301518.sql";
                yield return "cms_sp_UpdateUser201206301518.sql";
                yield return "cms_sp_UsersSelectForUserGroup201206301518.sql";
                yield return "UsersAddComputedUserName201206301518.sql";
            }
        }

        protected override IEnumerable<Action<IDatabase>> UpActions
        {
            get { yield return ConcreteUp; }
        }

        protected override IEnumerable<Action<IDatabase>> DownActions
        {
            get { yield return ConcreteDown; }
        }

        private void ConcreteUp(IDatabase db)
        {
            db.Tables["Users"].Columns["UserName"].Rename("Firstname");                
            
            db.Tables["Users"].AddNullableColumn("Lastname", DbType.String).OfSize(255);            
        }

        private void ConcreteDown(IDatabase db)
        {
            db.Tables["Users"].Columns["Firstname"].Rename("UserName");                        
        }
    }
}